﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Document_Management
{
    public partial class ManualToolMnt_Use : Form
    {
        static string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["庫存管理"].ConnectionString;
        string str = null;
        string 項次, 手工具編號, 手工具名稱, 領用日期, 領用部門, 領用人, 受理人, 歸還日期, 領用數量;
        bool b手工具Search, b手工具Insert, b手工具Edit, b手工具Del;
        public ManualToolMnt_Use(bool b手工具Search, bool b手工具Insert, bool b手工具Edit, bool b手工具Del)
        {
            InitializeComponent();
            this.b手工具Search = b手工具Search;
            this.b手工具Insert = b手工具Insert;
            this.b手工具Edit = b手工具Edit;
            this.b手工具Del = b手工具Del;
        }

        private void bt_手工具領用部門_新增_Click(object sender, EventArgs e)
        {
            try
            {
                if (txt_部門代號.Text.ToString() == null || txt_部門代號.Text.ToString().Equals(""))
                    MessageBox.Show("部門代號還沒輸入資料");
                else
                {
                    SqlConnection con = new SqlConnection(connectionString);
                    con.Open();
                    SqlCommand cmd0;
                    String sql = "SELECT COUNT(*) FROM 手工具領用部門 WHERE 部門代號='" + txt_部門代號.Text.ToString() + "'";
                    cmd0 = new SqlCommand(sql, con);
                    if ((int)cmd0.ExecuteScalar() > 0)
                    {
                        MessageBox.Show("Error! '" + txt_部門代號.Text.ToString() + "' 由來");
                    }
                    else
                    {
                        cmd0 = new SqlCommand("sp_手工具領用部門_insert", con);
                        cmd0.CommandType = CommandType.StoredProcedure;
                        cmd0.Parameters.AddWithValue("@部門代號", txt_部門代號.Text.ToString());
                        cmd0.Parameters.AddWithValue("@部門名稱", txt_部門名稱.Text.ToString());
                        cmd0.ExecuteNonQuery();
                        MessageBox.Show("新增成功");
                        Load_手工具領用部門();
                    }
                    con.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return;
            }
            //Load_txt_手工具領用部門代號();
        }

        private void bt_手工具領用部門_修改_Click(object sender, EventArgs e)
        {
            try
            {
                if (txt_部門代號.Text.ToString() == null || txt_部門代號.Text.ToString().Equals(""))
                    MessageBox.Show("部門代號還沒輸入資料");
                else
                {
                    SqlConnection con = new SqlConnection(connectionString);
                    con.Open();
                    SqlCommand cmd0;
                    String sql = "SELECT COUNT(*) FROM 手工具領用部門 WHERE 部門代號='" + str + "'";
                    cmd0 = new SqlCommand(sql, con);
                    if ((int)cmd0.ExecuteScalar() > 0)
                    {
                        cmd0 = new SqlCommand("sp_手工具領用部門_update", con);
                        cmd0.CommandType = CommandType.StoredProcedure;
                        cmd0.Parameters.AddWithValue("@部門代號_1", str);
                        cmd0.Parameters.AddWithValue("@部門代號_2", txt_部門代號.Text.ToString());
                        cmd0.Parameters.AddWithValue("@部門名稱", txt_部門名稱.Text.ToString());
                        cmd0.ExecuteNonQuery();
                        MessageBox.Show("修改成功");
                        Load_手工具領用部門();
                    }
                    else
                    {
                        MessageBox.Show("Error! '" + str + "' 不由來");                      
                    }
                    con.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return;
            }
        }

        private void bt_手工具領用部門_刪除_Click(object sender, EventArgs e)
        {
            try
            {
                if (txt_部門代號.Text.ToString() == null || txt_部門代號.Text.ToString().Equals(""))
                    MessageBox.Show("部門代號還沒輸入資料");
                else
                {
                    SqlConnection con = new SqlConnection(connectionString);
                    con.Open();
                    SqlCommand cmd0;
                    String sql = "SELECT COUNT(*) FROM 手工具領用部門 WHERE 部門代號='" + txt_部門代號.Text.ToString() + "'";
                    cmd0 = new SqlCommand(sql, con);
                    if ((int)cmd0.ExecuteScalar() > 0)
                    {
                        cmd0 = new SqlCommand("sp_手工具領用部門_delete", con);
                        cmd0.CommandType = CommandType.StoredProcedure;
                        cmd0.Parameters.AddWithValue("@部門代號", txt_部門代號.Text.ToString());
                        cmd0.ExecuteNonQuery();
                        MessageBox.Show("刪除成功");
                        Load_手工具領用部門();
                    }
                    else
                    {
                        MessageBox.Show("Error! '" + txt_部門代號.Text.ToString() + "' 不由來");
                    }
                    con.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return;
            }
        }

        private void bt_手工具領用部門_取消_Click(object sender, EventArgs e)
        {
            txt_部門代號.Text = "";
            txt_部門名稱.Text = "";
            Load_手工具領用部門();
        }

        public void Load_手工具領用部門()
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter("sp_手工具領用部門_load", con);
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                DataTable dt = new DataTable();
                da.Fill(dt);
                dataGrid_手工具領用部門.DataSource = dt;
                PublicClass.setRowNumber(dataGrid_手工具領用部門);
                dataGrid_手工具領用部門.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                con.Close();
                txt_部門代號.Text = "";
                txt_部門名稱.Text = "";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                con.Close();
            }
        }

        public void Load_手工具領用人()
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter("sp_手工具領用人_load", con);
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                DataTable dt = new DataTable();
                da.Fill(dt);
                dataGrid_手工具領用人.DataSource = dt;
                PublicClass.setRowNumber(dataGrid_手工具領用人);
                dataGrid_手工具領用人.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                con.Close();
                txt_領用人代號.Text = "";
                txt_領用人姓名.Text = "";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                con.Close();
            }
        }

        public void Load_手工具受理人()
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter("sp_手工具受理人_load", con);
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                DataTable dt = new DataTable();
                da.Fill(dt);
                dataGrid_手工具受理人.DataSource = dt;
                PublicClass.setRowNumber(dataGrid_手工具受理人);
                dataGrid_手工具受理人.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                con.Close();
                txt_受理人代號.Text = "";
                txt_受理人姓名.Text = "";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                con.Close();
            }
        }

        public void Load_手工具領用()
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter("sp_手工具領用_load", con);
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                DataTable dt = new DataTable();
                da.Fill(dt);
                dataGrid_領用.DataSource = dt;
                PublicClass.setRowNumber(dataGrid_領用);
                dataGrid_領用.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                con.Close();
            }
        }

        private void bt_手工具受理人_新增_Click(object sender, EventArgs e)
        {
            try
            {
                if (txt_受理人代號.Text.ToString() == null || txt_受理人代號.Text.ToString().Equals(""))
                    MessageBox.Show("受理人代號還沒輸入資料");
                else
                {
                    SqlConnection con = new SqlConnection(connectionString);
                    con.Open();
                    SqlCommand cmd0;
                    String sql = "SELECT COUNT(*) FROM 手工具受理人 WHERE 受理人代號='" + txt_受理人代號.Text.ToString() + "'";
                    cmd0 = new SqlCommand(sql, con);
                    if ((int)cmd0.ExecuteScalar() > 0)
                    {
                        MessageBox.Show("Error! '" + txt_受理人代號.Text.ToString() + "' 由來");
                    }
                    else
                    {
                        cmd0 = new SqlCommand("sp_手工具受理人_insert", con);
                        cmd0.CommandType = CommandType.StoredProcedure;
                        cmd0.Parameters.AddWithValue("@受理人代號", txt_受理人代號.Text.ToString());
                        cmd0.Parameters.AddWithValue("@受理人姓名", txt_受理人姓名.Text.ToString());
                        cmd0.ExecuteNonQuery();
                        MessageBox.Show("新增成功");
                        Load_手工具受理人();
                    }
                    con.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return;
            }
        }

        private void bt_手工具受理人_修改_Click(object sender, EventArgs e)
        {
            try
            {
                if (txt_受理人代號.Text.ToString() == null || txt_受理人代號.Text.ToString().Equals(""))
                    MessageBox.Show("受理人代號還沒輸入資料");
                else
                {
                    SqlConnection con = new SqlConnection(connectionString);
                    con.Open();
                    SqlCommand cmd0;
                    String sql = "SELECT COUNT(*) FROM 手工具受理人 WHERE 受理人代號='" + str + "'";
                    cmd0 = new SqlCommand(sql, con);
                    if ((int)cmd0.ExecuteScalar() > 0)
                    {
                        cmd0 = new SqlCommand("sp_手工具受理人_update", con);
                        cmd0.CommandType = CommandType.StoredProcedure;
                        cmd0.Parameters.AddWithValue("@受理人代號_1", str);
                        cmd0.Parameters.AddWithValue("@受理人代號_2", txt_受理人代號.Text.ToString());
                        cmd0.Parameters.AddWithValue("@受理人姓名", txt_受理人姓名.Text.ToString());
                        cmd0.ExecuteNonQuery();
                        MessageBox.Show("修改成功");
                        Load_手工具受理人();
                    }
                    else
                    {
                        MessageBox.Show("Error! '" + str + "' 不由來");
                    }
                    con.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return;
            }
        }

        private void bt_手工具受理人_刪除_Click(object sender, EventArgs e)
        {
            try
            {
                if (txt_受理人代號.Text.ToString() == null || txt_受理人代號.Text.ToString().Equals(""))
                    MessageBox.Show("受理人代號還沒輸入資料");
                else
                {
                    SqlConnection con = new SqlConnection(connectionString);
                    con.Open();
                    SqlCommand cmd0;
                    String sql = "SELECT COUNT(*) FROM 手工具受理人 WHERE 受理人代號='" + txt_受理人代號 + "'";
                    cmd0 = new SqlCommand(sql, con);
                    if ((int)cmd0.ExecuteScalar() > 0)
                    {
                        cmd0 = new SqlCommand("sp_手工具受理人_delete", con);
                        cmd0.CommandType = CommandType.StoredProcedure;
                        cmd0.Parameters.AddWithValue("@受理人代號", txt_受理人代號.Text.ToString());
                        cmd0.ExecuteNonQuery();
                        MessageBox.Show("刪除成功");
                        Load_手工具受理人();
                    }
                    else
                    {
                        MessageBox.Show("Error! '" + txt_受理人代號 + "' 不由來");
                    }
                    con.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return;
            }
        }

        private void bt_手工具受理人_存檔_Click(object sender, EventArgs e)
        {
            try
            {
                SqlConnection con = new SqlConnection(connectionString);
                con.Open();
                string sp = "sp_手工具受理人_search";
                SqlCommand cmd0 = new SqlCommand(sp, con);
                cmd0.CommandType = CommandType.StoredProcedure;
                if (txt_受理人代號.Text != "") cmd0.Parameters.AddWithValue("@受理人代號", txt_受理人代號.Text.ToString());
                if (txt_受理人姓名.Text != "") cmd0.Parameters.AddWithValue("@受理人姓名", txt_受理人姓名.Text.ToString());
                SqlDataReader sqlreader = cmd0.ExecuteReader();
                DataTable dt = new DataTable();

                dt.Columns.Add("受理人代號");
                dt.Columns.Add("受理人姓名");

                while (sqlreader.Read())
                {
                    DataRow dtrows = dt.NewRow();
                    dtrows["受理人代號"] = sqlreader["受理人代號"];
                    dtrows["受理人姓名"] = sqlreader["姓名"];
                    dt.Rows.Add(dtrows);
                }

                dataGrid_手工具受理人.DataSource = dt;
                PublicClass.setRowNumber(dataGrid_手工具受理人);
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void bt_手工具受理人_取消_Click(object sender, EventArgs e)
        {
            txt_受理人代號.Text = "";
            txt_受理人姓名.Text = "";
            Load_手工具受理人();
        }

        private void bt_手工具領用人_存檔_Click(object sender, EventArgs e)
        {
            try
            {
                SqlConnection con = new SqlConnection(connectionString);
                con.Open();
                string sp = "sp_手工具領用人_search";
                SqlCommand cmd0 = new SqlCommand(sp, con);
                cmd0.CommandType = CommandType.StoredProcedure;
                if (txt_領用人代號.Text != "") cmd0.Parameters.AddWithValue("@領用人代號", txt_領用人代號.Text.ToString());
                if (txt_手工具領用部門代號.Text != "") cmd0.Parameters.AddWithValue("@領用部門代號", txt_手工具領用部門代號.Text.ToString());
                if (txt_領用人姓名.Text != "") cmd0.Parameters.AddWithValue("@領用人姓名", txt_領用人姓名.Text.ToString());
                SqlDataReader sqlreader = cmd0.ExecuteReader();
                DataTable dt = new DataTable();

                dt.Columns.Add("領用人代號");
                dt.Columns.Add("領用部門代號");
                dt.Columns.Add("領用人姓名");

                while (sqlreader.Read())
                {
                    DataRow dtrows = dt.NewRow();
                    dtrows["領用人代號"] = sqlreader["領用人代號"];
                    dtrows["領用部門代號"] = sqlreader["領用部門代號"];
                    dtrows["領用人姓名"] = sqlreader["領用人姓名"];
                    dt.Rows.Add(dtrows);
                }
                dataGrid_手工具領用人.DataSource = dt;
                PublicClass.setRowNumber(dataGrid_手工具領用人);
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void bt_手工具領用人_新增_Click(object sender, EventArgs e)
        {
            try
            {
                if (txt_領用人代號.Text.ToString() == null || txt_領用人代號.Text.ToString().Equals(""))
                    MessageBox.Show("領用人代號還沒輸入資料");
                else
                {
                    SqlConnection con = new SqlConnection(connectionString);
                    con.Open();
                    SqlCommand cmd0;
                    String sql = "SELECT COUNT(*) FROM 手工具領用人 WHERE 領用人代號='" + txt_領用人代號.Text.ToString() + "'";
                    cmd0 = new SqlCommand(sql, con);
                    if ((int)cmd0.ExecuteScalar() > 0)
                    {
                        MessageBox.Show("Error! '" + txt_領用人代號.Text.ToString() + "' 由來");
                    }
                    else
                    {
                        cmd0 = new SqlCommand("sp_手工具領用人_insert", con);
                        cmd0.CommandType = CommandType.StoredProcedure;
                        cmd0.Parameters.AddWithValue("@領用人代號", txt_領用人代號.Text.ToString());
                        cmd0.Parameters.AddWithValue("@領用人姓名", txt_領用人姓名.Text.ToString());
                        cmd0.Parameters.AddWithValue("@領用部門代號", txt_手工具領用部門代號.Text.ToString());
                        cmd0.ExecuteNonQuery();
                        MessageBox.Show("新增成功");
                        Load_手工具領用人();
                    }
                    con.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return;
            }
        }

        private void bt_手工具領用人_修改_Click(object sender, EventArgs e)
        {
            try
            {
                if (txt_領用人代號.Text.ToString() == null || txt_領用人代號.Text.ToString().Equals(""))
                    MessageBox.Show("領用人代號還沒輸入資料");
                else
                {
                    SqlConnection con = new SqlConnection(connectionString);
                    con.Open();
                    SqlCommand cmd0;
                    String sql = "SELECT COUNT(*) FROM 手工具領用人 WHERE 領用人代號='" + str + "'";
                    cmd0 = new SqlCommand(sql, con);
                    if ((int)cmd0.ExecuteScalar() > 0)
                    {
                        cmd0 = new SqlCommand("sp_手工具領用人_update", con);
                        cmd0.CommandType = CommandType.StoredProcedure;
                        cmd0.Parameters.AddWithValue("@領用人代號_1", str);
                        cmd0.Parameters.AddWithValue("@領用人代號_2", txt_領用人代號.Text.ToString());
                        cmd0.Parameters.AddWithValue("@領用人姓名", txt_領用人姓名.Text.ToString());
                        cmd0.Parameters.AddWithValue("@領用部門代號", txt_手工具領用部門代號.Text.ToString());
                        cmd0.ExecuteNonQuery();
                        MessageBox.Show("修改成功");
                        Load_手工具領用人();
                    }
                    else
                    {
                        MessageBox.Show("Error! '" + str + "' 不由來");                       
                    }
                    con.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return;
            }
        }

        private void bt_手工具領用人_刪除_Click(object sender, EventArgs e)
        {
            try
            {
                if (txt_領用人代號.Text.ToString() == null || txt_領用人代號.Text.ToString().Equals(""))
                    MessageBox.Show("領用人代號還沒輸入資料");
                else
                {
                    SqlConnection con = new SqlConnection(connectionString);
                    con.Open();
                    SqlCommand cmd0;
                    String sql = "SELECT COUNT(*) FROM 手工具領用人 WHERE 領用人代號='" + str + "'";
                    cmd0 = new SqlCommand(sql, con);
                    if ((int)cmd0.ExecuteScalar() > 0)
                    {
                        cmd0 = new SqlCommand("sp_手工具領用人_delete", con);
                        cmd0.CommandType = CommandType.StoredProcedure;
                        //cmd0.Parameters.AddWithValue("@領用人代號_1", str);
                        cmd0.Parameters.AddWithValue("@領用人代號", txt_領用人代號.Text.ToString());
                        //cmd0.Parameters.AddWithValue("@領用人姓名", txt_領用人姓名.Text.ToString());
                        //cmd0.Parameters.AddWithValue("@領用部門代號", txt_手工具領用部門代號.Text.ToString());
                        cmd0.ExecuteNonQuery();
                        MessageBox.Show("刪除成功");
                        Load_手工具領用人();
                    }
                    else
                    {
                        MessageBox.Show("Error! '" + str + "' 不由來");
                    }
                    con.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return;
            }
        }

        private void bt_手工具領用人_取消_Click(object sender, EventArgs e)
        {
            txt_領用人代號.Text = "";
            txt_領用人姓名.Text = "";
            txt_手工具領用部門代號.Text = "";
            Load_手工具領用人();
        }

        private void bt_搜尋_Click(object sender, EventArgs e)
        {
            try
            {
                SqlConnection con = new SqlConnection(connectionString);
                con.Open();
                string sp = "sp_手工具領用_search";
                SqlCommand cmd0 = new SqlCommand(sp, con);
                cmd0.CommandType = CommandType.StoredProcedure;
                //if (check_領用日期.Checked.Equals(true))
                //{
                    cmd0.Parameters.AddWithValue("@領用日期_1", dateTime_領用日期_1.Value);
                    cmd0.Parameters.AddWithValue("@領用日期_2", dateTime_領用日期_2.Value);
                //}
                //if (check_歸還日期.Checked.Equals(true))
                //{
                    cmd0.Parameters.AddWithValue("@歸還日期_1", dateTime_歸還日期_1.Value);
                    cmd0.Parameters.AddWithValue("@歸還日期_2", dateTime_歸還日期_2.Value);
                //}    
                if (txt_手工具名稱.Text != "") cmd0.Parameters.AddWithValue("@手工具名稱", txt_手工具名稱.Text.ToString());
                if (txt_領用部門.Text != "") cmd0.Parameters.AddWithValue("@領用部門", txt_領用部門.Text.ToString());
                if (txt_手工具編號.Text != "") cmd0.Parameters.AddWithValue("@手工具編號", txt_手工具編號.Text.ToString());
                if (txt_受理人.Text != "") cmd0.Parameters.AddWithValue("@受理人", txt_受理人.Text.ToString());

                SqlDataReader sqlreader = cmd0.ExecuteReader();
                DataTable dt = new DataTable();
                //手工具領用.項次,手工具.手工具編號,手工具.手工具名稱, 手工具.規格型號,手工具領用部門.部門名稱 AS 領用部門, 
		        //手工具領用人.姓名 AS 領用人, 手工具受理人.姓名 AS 受理人, 手工具領用.數量,手工具領用.領用日期,手工具領用.歸還日期, 手工具領用.備註

                dt.Columns.Add("項次");
                dt.Columns.Add("手工具編號");
                dt.Columns.Add("手工具名稱");
                dt.Columns.Add("規格型號");
                dt.Columns.Add("領用部門");
                dt.Columns.Add("領用人");
                dt.Columns.Add("受理人");
                dt.Columns.Add("數量");
                dt.Columns.Add("領用日期");
                dt.Columns.Add("歸還日期");
                dt.Columns.Add("備註");

                while (sqlreader.Read())
                {
                    DataRow dtrows = dt.NewRow();
                    dtrows["項次"] = sqlreader["項次"];
                    dtrows["手工具編號"] = sqlreader["手工具編號"];
                    dtrows["手工具名稱"] = sqlreader["手工具名稱"];
                    dtrows["規格型號"] = sqlreader["規格型號"];
                    dtrows["領用部門"] = sqlreader["領用部門"];
                    dtrows["領用人"] = sqlreader["領用人"];
                    dtrows["受理人"] = sqlreader["受理人"];
                    dtrows["數量"] = sqlreader["數量"];
                    dtrows["領用日期"] = sqlreader["領用日期"];
                    dtrows["歸還日期"] = sqlreader["歸還日期"];
                    dtrows["備註"] = sqlreader["備註"];
                    dt.Rows.Add(dtrows);
                }

                dataGrid_領用.DataSource = dt;
                PublicClass.setRowNumber(dataGrid_領用);
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                //con.Close();
            }
        }

        private void ManualToolMnt_Use_Load(object sender, EventArgs e)
        {
            Load_手工具領用();
            if (!b手工具Edit)
            {
                bt_歸還.Enabled = false;
                bt_手工具受理人_修改.Enabled = false;
                bt_手工具領用人_修改.Enabled = false;
                bt_手工具領用部門_修改.Enabled = false;
            }
            if (!b手工具Search)
            {
                bt_搜尋.Enabled = false;
                bt_手工具受理人_存檔.Enabled = false;
                bt_手工具領用人_存檔.Enabled = false;
            }
            if (!b手工具Insert)
            {
                bt_歸還.Enabled = false;
                bt_手工具受理人_新增.Enabled = false;
                bt_手工具領用人_新增.Enabled = false;
                bt_手工具領用部門_新增.Enabled = false;
            }
            if (!b手工具Del)
            {
                bt_歸還.Enabled = false;
                bt_手工具受理人_刪除.Enabled = false;
                bt_手工具領用人_刪除.Enabled = false;
                bt_手工具領用部門_刪除.Enabled = false;
            }
        }

        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (tabControl1.SelectedIndex == 0)
            {
                Load_手工具領用();
                PublicClass.setRowNumber(dataGrid_領用);
            }
            if (tabControl1.SelectedIndex == 1)
            {
                Load_手工具受理人();
                PublicClass.setRowNumber(dataGrid_手工具受理人);
            }
            if (tabControl1.SelectedIndex == 2)
            {
                Load_手工具領用人();
                PublicClass.setRowNumber(dataGrid_手工具領用人);
            }
            if (tabControl1.SelectedIndex == 3)
            {
                Load_手工具領用部門();
                PublicClass.setRowNumber(dataGrid_手工具領用部門);
            }
        }

        private void bt_歸還_Click(object sender, EventArgs e)
        {
            ManualTool_LingYong_Return frm = new ManualTool_LingYong_Return("領用", 項次, 手工具編號, 手工具名稱, 領用日期, 領用部門, 領用人, 受理人, 歸還日期, 領用數量);
            frm.ShowDialog();
            Load_手工具領用();
        }

        private void dataGrid_領用_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            DataGridViewRow row = new DataGridViewRow();
            row = dataGrid_領用.Rows[e.RowIndex];
            項次 = row.Cells[0].Value.ToString();
            手工具編號 = row.Cells[1].Value.ToString();
            手工具名稱 = row.Cells[2].Value.ToString();

            領用部門 = row.Cells[4].Value.ToString(); 
            領用人 = row.Cells[5].Value.ToString();
            受理人 = row.Cells[6].Value.ToString();
            領用數量 = row.Cells[7].Value.ToString();
            領用日期 = row.Cells[8].Value.ToString();
            歸還日期 = row.Cells[9].Value.ToString();
        }

        private void bt_重置_Click(object sender, EventArgs e)
        {
            Load_手工具領用();
            dateTime_歸還日期_1.Value = dateTime_歸還日期_1.MinDate;
            dateTime_歸還日期_2.Value = dateTime_歸還日期_2.MaxDate;
            dateTime_領用日期_1.Value = dateTime_領用日期_1.MinDate;
            dateTime_領用日期_2.Value = dateTime_領用日期_2.MaxDate;
            txt_手工具編號.Text = "";
            txt_領用部門.Text = "";
            txt_手工具名稱.Text = "";
            txt_受理人.Text = "";
        }

        private void dataGrid_手工具受理人_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            DataGridViewRow row = new DataGridViewRow();
            row = dataGrid_手工具受理人.Rows[e.RowIndex];
            txt_受理人代號.Text = row.Cells[0].Value.ToString();
            txt_受理人姓名.Text = row.Cells[1].Value.ToString();
        }

        private void dataGrid_手工具領用人_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            DataGridViewRow row = new DataGridViewRow();
            row = dataGrid_手工具領用人.Rows[e.RowIndex];
            txt_領用人代號.Text = row.Cells[0].Value.ToString();
            str = row.Cells[0].Value.ToString();
            txt_領用人姓名.Text = row.Cells[1].Value.ToString();
        }

        private void dataGrid_手工具領用部門_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            DataGridViewRow row = new DataGridViewRow();
            row = dataGrid_手工具領用部門.Rows[e.RowIndex];
            txt_部門代號.Text = row.Cells[0].Value.ToString();
            str = row.Cells[0].Value.ToString();
            txt_部門名稱.Text = row.Cells[1].Value.ToString();
        }

        private void tabP_手工具領用人_Click(object sender, EventArgs e)
        {

        }

        private void bt_領用部門代號_search_Click(object sender, EventArgs e)
        {
            VehicleSearch frm = new VehicleSearch(txt_手工具領用部門代號);
            frm.ShowDialog();
        }

        private void txt_手工具領用部門代號_TextChanged(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                String temp = "";
                string strcmd = String.Format("select * from 手工具領用部門 where 部門代號=N'{0}'", txt_手工具領用部門代號.Text);
                SqlCommand cmd = new SqlCommand(strcmd, con);
                var x = cmd.ExecuteReader();
                while (x.Read()) temp = x[1].ToString();
                if (temp == "") lb_領用部門代號.Text = "";
                else lb_領用部門代號.Text = temp;
                con.Close();
            }
            catch (Exception ex) 
            {
                MessageBox.Show("Connect error !");
            }
        }      
    }
}
